<template>
  <el-form ref="form" :model="user" :rules="rules" label-width="auto">
    <el-form-item :label="$t('menu39')" prop="oldPassword">
      <el-input v-model="user.oldPassword" :placeholder="$t('menu42')" show-password type="password"/>
    </el-form-item>
    <el-form-item :label="$t('menu40')" prop="newPassword">
      <el-input v-model="user.newPassword" :placeholder="$t('menu43')" show-password type="password"/>
    </el-form-item>
    <el-form-item :label="$t('menu41')" prop="confirmPassword">
      <el-input v-model="user.confirmPassword" :placeholder="$t('menu43')" show-password type="password"/>
    </el-form-item>
    <el-form-item>
      <el-button size="mini" type="primary" @click="submit">{{ $t('ok') }}</el-button>
      <el-button size="mini" type="danger" @click="close">{{ $t('cancel') }}</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import {updateUserPwd} from "@/api/system/user";

export default {
  data() {
    const equalToPassword = (rule, value, callback) => {
      if (this.user.newPassword !== value) {
        callback(new Error(this.$t('menu44')));
      } else {
        callback();
      }
    };
    return {
      user: {
        oldPassword: undefined,
        newPassword: undefined,
        confirmPassword: undefined
      },
      // 表单校验
      rules: {
        oldPassword: [
          {required: true, message: this.$t('menu45'), trigger: "blur"}
        ],
        newPassword: [
          {required: true, message: this.$t('menu46'), trigger: "blur"},
          {min: 6, max: 20, message: this.$t('menu47'), trigger: "blur"},
          {pattern: /^[^<>"'|\\]+$/, message: this.$t('menu48'), trigger: "blur"}
        ],
        confirmPassword: [
          {required: true, message: this.$t('menu49'), trigger: "blur"},
          {required: true, validator: equalToPassword, trigger: "blur"}
        ]
      }
    };
  },
  methods: {
    submit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          updateUserPwd(this.user.oldPassword, this.user.newPassword).then(response => {
            this.$modal.msgSuccess(this.$t('modifiedSuccessfully'));
          });
        }
      });
    },
    close() {
      this.$tab.closePage();
    }
  }
};
</script>
